package com.noah.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class XmlServlet
 */
public class XmlServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public XmlServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// response.getWriter().write("<body><h3>Run servlet with config web.xml, without @WebServlet</h3></body>");
		response.setContentType("text/html");
		String userName = request.getParameter("userName");
		String id = request.getParameter("id");
		response.getWriter().println("GET method");
		response.getWriter().println(
				"<body><h3>Run servlet with config web.xml, without @WebServlet: "
						+ userName + "------Id: " + id + "</h3></body>");
		// Kết quả:
		// http://localhost:8080/englishResearch/XmlServletPath?userName=noah&id=1
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		String userName = request.getParameter("userName");
		String id = request.getParameter("id");
		PrintWriter wr = response.getWriter();
		response.getWriter().println("POST method");
		response.getWriter().println(
				"<body><h3>Run servlet with config web.xml, without @WebServlet: "
						+ userName + "------Id: " + id + "</h3></body>");
		String choose = request.getParameter("rad");
		wr.println("You chose: " + choose);
		// Dùng lệnh dưới cho 1 loc được chọn
		// String loc = request.getParameter("location");
		String[] loc = request.getParameterValues("location");
		wr.println("You're location : " + loc.length + " are: ");
		for (String l : loc) {
			wr.println(l);
		}

	}

}
